using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using ADODB;

namespace DWSchema
{
	/// <summary>
	/// Summary description for Class.
	/// </summary>
	public class Class : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.ListBox listBox2;
		private System.Windows.Forms.Button button1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		
		internal string className = string.Empty;
		private Connection con = null;

		public Class(string server, string database, string user, string password)
		{
			InitializeComponent();
			con = new ConnectionClass();
			con.Open("URL=mscop://InProcConnect/server="+server+":database="+database+":Catalog=DWSchema: user="+user+":password="+password+":FastLoad=True", string.Empty, string.Empty, 0);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.label2 = new System.Windows.Forms.Label();
			this.listBox2 = new System.Windows.Forms.ListBox();
			this.button1 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(184, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "Class Properties:";
			// 
			// listBox1
			// 
			this.listBox1.Location = new System.Drawing.Point(0, 40);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(368, 147);
			this.listBox1.Sorted = true;
			this.listBox1.TabIndex = 1;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 200);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(184, 16);
			this.label2.TabIndex = 2;
			this.label2.Text = "Data Members:";
			// 
			// listBox2
			// 
			this.listBox2.Location = new System.Drawing.Point(0, 224);
			this.listBox2.Name = "listBox2";
			this.listBox2.Size = new System.Drawing.Size(368, 147);
			this.listBox2.Sorted = true;
			this.listBox2.TabIndex = 3;
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(304, 8);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(64, 23);
			this.button1.TabIndex = 4;
			this.button1.Text = "&Close";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// Class
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(370, 375);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.listBox2);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.listBox1);
			this.Controls.Add(this.label1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Class";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Class";
			this.Load += new System.EventHandler(this.Class_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void button1_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void Class_Load(object sender, System.EventArgs e)
		{
			// load properties
			this.Text = "Class Properties: " + className;
    
			LoadList(@"class/" + className, listBox1);
			LoadList(@"instance/" + className, listBox2);
		}

		/// <summary>
		/// Loads a list box with all the data in for a data warehouse object.
		/// </summary>
		/// <param name="strURL">The data warehouse url of the object to load in the list box.</param>
		/// <param name="lst">The list box to fill with the data from the data warehouse object.</param>
		private void LoadList(string strURL, ListBox lst)
		{
			try
			{
				Record oClass = new RecordClass();
				string strVal = string.Empty;
				
				oClass.Open(strURL, con, ConnectModeEnum.adModeReadWrite, RecordCreateOptionsEnum.adCreateOverwrite, RecordOpenOptionsEnum.adOpenRecordUnspecified, string.Empty, string.Empty);
				
				foreach(Field oFld in oClass.Fields)
				{
					if (!oFld.Name.StartsWith("_"))
					{
						if(oFld.Type.ToString() != "adEmpty" && oFld.Value!=null)
						{
							strVal = oFld.Value.ToString();
						}
						else
						{
							strVal = "";
						}

						if (strVal.Length  > 0)
							strVal = ": " + strVal;
						lst.Items.Add(oFld.Name + strVal);
					}
					
				}
    
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}
	}
}
